package vip.xiaonuo.app.modular.oss;

import com.aliyuncs.auth.sts.AssumeRoleResponse;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.app.core.aliyun.sts.OssStsUtil;
import vip.xiaonuo.app.core.aliyun.sts.StsDto;
import vip.xiaonuo.app.core.aliyun.sts.enums.OssStsEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.sys.modular.file.param.front.CallBackFileInfoParam;
import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;

import javax.annotation.Resource;


/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/10/7 19:37
 */
@RestController
@RequestMapping("/api/ossSts")
public class OssSTSController {

    @Resource
    private SysFileInfoService sysFileInfoService;

    /**
     * 生成临时访问密钥
     */
    @GetMapping(value = "/getStsToken")
    public ResponseData getStsToken() {

        AssumeRoleResponse response = OssStsUtil.getOssSTS();
        String securityToken = response.getCredentials().getSecurityToken();
        String accessKeySecret = response.getCredentials().getAccessKeySecret();
        String accessKeyId = response.getCredentials().getAccessKeyId();

        StsDto stsDto = new StsDto();
        stsDto.setSecurityToken(securityToken);
        stsDto.setAccessKeySecret(accessKeySecret);
        stsDto.setAccessKeyId(accessKeyId);

        stsDto.setEndpoint("https://oss-cn-shenzhen.aliyuncs.com");
        stsDto.setBucketName("zyprod");
        stsDto.setTrends(OssStsEnum.TRENDS.getName());
        stsDto.setUserinfo(OssStsEnum.USER_INFO.getName());

        return ResponseData.success(stsDto);
    }


    /**
     * 保存文件信息回调
     *
     * @param param
     * @return
     */
    @PostMapping("/callBackFiInfo")
    public ResponseData callBackFiInfo(@RequestBody CallBackFileInfoParam param) {
        Long aLong = sysFileInfoService.callBackFileInfo(param);
        return new SuccessResponseData(aLong);
    }
}
